package com.ruoyi.wlxz.controller;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wlxz.domain.Filerecord;
import com.ruoyi.wlxz.service.IFilerecordService;
import com.ruoyi.wlxz.tools.FileManager;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
 * 文档Controller
 *
 * @author zlh
 * @date 2025-03-22
 */
@RestController
@RequestMapping("/wlxz/filerecord")
public class FilerecordController extends BaseController {
    @Autowired
    private IFilerecordService filerecordService;
    @Autowired
    private FileManager fileManager;
    /**
     * 查询文档列表
     */
    @PreAuthorize("@ss.hasPermi('wlxz:filerecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(Filerecord filerecord) {
        startPage();
        List<Filerecord> list = filerecordService.selectFilerecordList(filerecord);
        return getDataTable(list);
    }
    /**
     * 导出文档列表
     */
    @PreAuthorize("@ss.hasPermi('wlxz:filerecord:export')")
    @Log(title = "文档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Filerecord filerecord) {
        List<Filerecord> list = filerecordService.selectFilerecordList(filerecord);
        ExcelUtil<Filerecord> util = new ExcelUtil<Filerecord>(Filerecord.class);
        util.exportExcel(response, list, "文档数据");
    }
    /**
     * 获取文档详细信息
     */
    @PreAuthorize("@ss.hasPermi('wlxz:filerecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(filerecordService.selectFilerecordById(id));
    }
    /**
     * 新增文档
     */
    @PreAuthorize("@ss.hasPermi('wlxz:filerecord:add')")
    @Log(title = "文档", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@RequestParam("cartegoryId") String cartegoryId,
                          @RequestParam("file") MultipartFile file) throws Exception {
        String fileId = fileManager.ApplyFileUploadLease(cartegoryId, file);
        Filerecord filerecord = new Filerecord();
        filerecord.setCreateTime(LocalDateTime.now());
        filerecord.setCreateBy(getUsername());
        filerecord.setFileId(fileId);
        filerecord.setFileName(FilenameUtils.removeExtension(file.getOriginalFilename()));
        filerecord.setFileSize(file.getSize());
        filerecord.setFileFormat(FilenameUtils.getExtension(file.getOriginalFilename()));
        filerecord.setCategoryId(cartegoryId);
        return toAjax(filerecordService.insertFilerecord(filerecord));
    }
    /**
     * 修改文档
     */
    @PreAuthorize("@ss.hasPermi('wlxz:filerecord:edit')")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Filerecord filerecord) {
        return toAjax(filerecordService.updateFilerecord(filerecord));
    }
    /**
     * 删除文档
     */
    @PreAuthorize("@ss.hasPermi('wlxz:filerecord:remove')")
    @Log(title = "文档", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<String> idsList = new ArrayList<>();
        for (Long id : ids) {
            //删除类目下的文档
            Filerecord filerecord = filerecordService.selectFilerecordById(id);
            String fileId = filerecord.getFileId();
            idsList.add(fileId);
            fileManager.DeleteFile(fileId);
        }
        //删除知识库中的文档
        fileManager.DeleteIndexDocument(idsList);
        //删除数据库中的文档
        return toAjax(filerecordService.deleteFilerecordByIds(ids));
    }
}
